package com.orion.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.context.annotation.Configuration;

/**
 * @author Administrator
 * @date 2021/10/25
 */
@Configuration
public class ApolloConfig {

    //每当有变化都触发
    //config change listener for namespace application
    @ApolloConfigChangeListener()
    private void anotherOnChange(ConfigChangeEvent changeEvent) {
        //config instance is singleton for each namespace and is never null
        System.out.println("Changes for namespace " + changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
        }
    }

    //单独监听一个属性变化来触发
    @ApolloConfigChangeListener()
    private void oneOnChange(ConfigChangeEvent changeEvent) {
        if (changeEvent.isChanged("code.orion")) {
            System.out.println("code.orion is change : " + changeEvent.getChange("code.orion"));
        }
    }
}
